package com.studystack.studyme;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

/*
 * This class is used to get and set the options for a J2ME Midlet
 */
public class Options
{
    public Options( String [] allOptionNames )
    {
        this.optionNames = allOptionNames ;
        int numOptions = allOptionNames.length ;
        this.optionValues = new String[ numOptions ];
        this.optionRecordIds = new int[ numOptions ];
        for ( int option=0; option<numOptions; option++ )
        {
            optionRecordIds[ option ] = -1;
        }
    }

    // load all options from RecordStore
    public void loadOptions() throws Exception
    {
        RecordStore optionStore = null ;
        try {
            optionStore = RecordStore.openRecordStore( RECORD_STORE_NAME, false );
            RecordEnumeration recEnum = optionStore.enumerateRecords( null, null, false );
            //System.out.println( "numRecords=" + recEnum.numRecords() );
            int recordId ;
            byte [] bytes ;
            while ( recEnum.hasNextElement() )
            {
                recordId = recEnum.nextRecordId();
                //System.out.println( "recordId=" + recordId );
                bytes = optionStore.getRecord( recordId );
                //System.out.println( "bytes.length = " + bytes.length );
                String recordString = new String( bytes );
                //System.out.println( "read: " + recordString );
                int eqPos = recordString.indexOf( '=' ) ;
                String optionName = recordString.substring( 0, eqPos );
                String optionValue ;
                if ( eqPos < recordString.length() )
                {
                    optionValue = recordString.substring( eqPos + 1 );
                }
                else 
                {
                    optionValue = "" ;
                }
                int pos = this.findOptionPosition( optionName );
                if ( pos >= 0 )
                {
                    optionValues[pos] = optionValue ;
                    optionRecordIds[pos] = recordId ;
                }
            }
            
        }
        catch ( RecordStoreNotFoundException e )
        {
            // that's okay
        }
        finally {
            if ( optionStore != null ) {
                optionStore.closeRecordStore();
            }
        }
        
    }
    
    public String getOption( String optionName )
    {
        int pos = findOptionPosition( optionName );
        
        return optionValues[ pos ];
    }
    
    public void setOption( String optionName, String optionValue ) throws RecordStoreFullException, RecordStoreNotFoundException, RecordStoreException
    {
        int pos = findOptionPosition( optionName );
    
        optionValues[pos] = optionValue ;
        
        // persist to RecordStore
        RecordStore optionStore = RecordStore.openRecordStore( RECORD_STORE_NAME, true );
        try {
            byte [] bytes = ( optionName + "=" + optionValue ).getBytes();
            int recordId = optionRecordIds[pos];
            if ( recordId<0)
            {
                // create new record
                recordId = optionStore.addRecord( bytes, 0, bytes.length );
                optionRecordIds[ pos ] = recordId ; 
            }
            else 
            {
                // update existing record
                optionStore.setRecord( recordId, bytes, 0, bytes.length );
            }
        }
        finally {
            optionStore.closeRecordStore();
        }
    }
    
    private int findOptionPosition( String optionName )
    {
        for ( int pos = optionNames.length - 1; pos>=0; pos-- )
        {
            if ( optionName.equals( optionNames[ pos ])) {
                return pos ;
            }
        }
        return -1;
    }
    
    
    private String[] optionNames ;
    private int[]  optionRecordIds ;
    private String[] optionValues ;
    public final static String RECORD_STORE_NAME = "$OPTS$" ; 
    
}
